﻿#include "LensManageDialog.h"
#include "LensManager.h"
#include "ColorComboBox.h"
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QAbstractButton>
#include <QPushButton>
#include <QFrame>
#include <QDebug>

namespace Qly
{
LensManageDialog::LensManageDialog(LensManager *l, QWidget *parent) :
    QDialog(parent)
{
    setupUi();
    lens = l;
    setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
    layout()->setSizeConstraint(QLayout::SetFixedSize);
    connect(comboBoxLensCount, SIGNAL(currentIndexChanged(int)), this, SLOT(updateLensCount(int)));
    if(l)    loadLensData(l);
}

void LensManageDialog::updateLensCount(int n)
{
    n = n + 1;
    qDebug() << "n = " << n;
    for(int i = 0; i < n; i++)
    {
        comboBoxLens[i]->setVisible(true);
        lineEditNA[i]->setVisible(true);
        lineEditWDC[i]->setVisible(true);
        lineEditName[i]->setVisible(true);
        lineEditUmPix[i]->setVisible(true);
        lineEditRulerLen[i]->setVisible(true);
        lineEditXCompensate[i]->setVisible(true);
        lineEditYCompensate[i]->setVisible(true);
    }
    for(int i = n; i < LENSCOUNT; i++)
    {
        comboBoxLens[i]->setVisible(false);
        lineEditNA[i]->setVisible(false);
        lineEditWDC[i]->setVisible(false);
        lineEditName[i]->setVisible(false);
        lineEditUmPix[i]->setVisible(false);
        lineEditRulerLen[i]->setVisible(false);
        lineEditXCompensate[i]->setVisible(false);
        lineEditYCompensate[i]->setVisible(false);
    }
}

void LensManageDialog::accept()
{
    // 检查数据合理性
    int n = comboBoxLensCount->currentIndex() + 1;
    for(int i = 0; i < n; i++)
    {
        if( lineEditName[i]->text().isEmpty() )
            lineEditName[i]->setText(u8"Empty");

        if( lineEditWDC[i]->text().isEmpty() )
            lineEditWDC[i]->setText(u8"1");

        if( lineEditUmPix[i]->text().isEmpty() )
            lineEditUmPix[i]->setText(u8"1");

        if( lineEditNA[i]->text().isEmpty() )
            lineEditNA[i]->setText(u8"0");

        if( lineEditXCompensate[i]->text().isEmpty() )
            lineEditXCompensate[i]->setText(u8"0");

        if( lineEditYCompensate[i]->text().isEmpty() )
            lineEditYCompensate[i]->setText(u8"0");

        if( lineEditRulerLen[i]->text().isEmpty() )
            lineEditRulerLen[i]->setText(u8"100");
    }
//     dumpLensData(lens);
     QDialog::accept();
//     emit lensDataChanged();
}

void LensManageDialog::loadLensData(LensManager * l)
{
    int n = l->lensCount();
    comboBoxLensCount->setCurrentIndex(n - 1);
    for(int i = 0; i < n; i++)
    {
        LensData &d = l->lensByIndex(i);
        lineEditName[i]->setText(d.displayName);
        comboBoxLens[i]->setCurrentIndex(d.lens);
        lineEditUmPix[i]->setText(QString("%1").arg(d.pixelSize_um));
        lineEditNA[i]->setText(QString("%1").arg(d.numericalAperture));
        lineEditWDC[i]->setText(QString("%1").arg(d.workDistance_um));
        lineEditXCompensate[i]->setText(QString("%1").arg(d.xCompensate_um));
        lineEditYCompensate[i]->setText(QString("%1").arg(d.yCompensate_um));
        lineEditRulerLen[i]->setText(QString("%1").arg(d.rulerLength_um));

        lineEditNA[i]->setVisible(true);
        lineEditWDC[i]->setVisible(true);
        lineEditXCompensate[i]->setVisible(true);
        lineEditYCompensate[i]->setVisible(true);
        lineEditName[i]->setVisible(true);
        lineEditUmPix[i]->setVisible(true);
        lineEditRulerLen[i]->setVisible(true);
    }
    for(int i = n; i < LENSCOUNT; i++)
    {
        lineEditNA[i]->setVisible(false);
        lineEditXCompensate[i]->setVisible(false);
        lineEditYCompensate[i]->setVisible(false);
        lineEditWDC[i]->setVisible(false);
        lineEditName[i]->setVisible(false);
        lineEditUmPix[i]->setVisible(false);
        lineEditRulerLen[i]->setVisible(false);
    }

    QPoint p = l->rulerPosition();
    lineEditRulerPosX->setText(QString("%1").arg(p.x()));
    lineEditRulerPosY->setText(QString("%1").arg(p.y()));

    int lineWidth = l->rulerLineWidth();
    int fontSize = l->rulerFontSize();

    lineEditLineWidth->setText(QString("%1").arg(l->rulerLineWidth()));
    lineEditFontSize->setText(QString("%1").arg(l->rulerFontSize()));

    Qt::GlobalColor color = l->rulerColor();
    int index = comboBoxColor->indexFromColor(QColor(color));
    comboBoxColor->setCurrentIndex(index);


}

void LensManageDialog::dumpLensData(LensManager * l)
{
    int n = comboBoxLensCount->currentIndex() + 1;
    for(int i = 0; i < n; i++)
    {

        LensData &d = l->lensByIndex(i);
        d.lens = (Qly::LENS)comboBoxLens[i]->currentIndex();
        d.displayName = lineEditName[i]->text();

        d.workDistance_um = lineEditWDC[i]->text().toDouble();
        d.pixelSize_um = lineEditUmPix[i]->text().toDouble();
        d.numericalAperture = lineEditNA[i]->text().toDouble();

        d.rulerText = lineEditName[i]->text();
        d.rulerLength_um = lineEditRulerLen[i]->text().toDouble();

        d.xCompensate_um = lineEditXCompensate[i]->text().toDouble();
        d.yCompensate_um = lineEditYCompensate[i]->text().toDouble();
        //l->setLensByIndex(i, d);
    }

    int x = lineEditRulerPosX->text().toInt();
    int y = lineEditRulerPosY->text().toInt();
    int width = lineEditLineWidth->text().toInt();
    int fontSize = lineEditFontSize->text().toInt();
    Qt::GlobalColor color = comboBoxColor->color();

    l->setRulerPosition(x, y);
    l->setRulerLineWidth(width);
    l->setRulerFontSize(fontSize);
    l->setRulerColor(color);
    l->setLensCount(n);
    int c = l->currentLensIndex();
    if(c >= l->lensCount())
    {
        l->lensChanged(c, 0);
    }
}

LensManageDialog::~LensManageDialog()
{
}

void LensManageDialog::setupUi()
{
    QHBoxLayout *horizontalLayout = new QHBoxLayout();
    QLabel *label1 = new QLabel(this);
    label1->setText(tr(u8"Lens Count："));
    horizontalLayout->addWidget(label1);

    comboBoxLensCount = new QComboBox(this);
    comboBoxLensCount->addItem(u8"1");
    comboBoxLensCount->addItem(u8"2");
    comboBoxLensCount->addItem(u8"3");
    comboBoxLensCount->addItem(u8"4");
    comboBoxLensCount->addItem(u8"5");
    comboBoxLensCount->addItem(u8"6");
    comboBoxLensCount->addItem(u8"7");
    comboBoxLensCount->addItem(u8"8");

    comboBoxLensCount->setMinimumSize(QSize(100, 0));
    horizontalLayout->addWidget(comboBoxLensCount);
    horizontalLayout->addStretch();

    QGridLayout* gridLayout = new QGridLayout();
    QLabel *label = new QLabel(this);
    label->setText(tr("Lens Type"));
    gridLayout->addWidget(label, 0, 1, 1, 1);

    QLabel *label2 = new QLabel(this);
    label2->setText(tr("Lens Name"));
    gridLayout->addWidget(label2, 0, 2, 1, 1);
    QLabel *label3 = new QLabel(this);
    label3->setText(tr(u8"Pixel Size(μm/Pix)"));
    gridLayout->addWidget(label3, 0, 3, 1, 1);
    QLabel *label4 = new QLabel(this);
    label4->setText(tr(u8"Numerical Aperture"));
    gridLayout->addWidget(label4, 0, 4, 1, 1);
    QLabel *label5 = new QLabel(this);
    label5->setText(tr(u8"Z Compensate(mm)"));
    gridLayout->addWidget(label5, 0, 5, 1, 1);
    QLabel *label6 = new QLabel(this);
    label6->setText(tr(u8"X Compensate(μm)"));
    gridLayout->addWidget(label6, 0, 6, 1, 1);
    QLabel *label7 = new QLabel(this);
    label7->setText(tr(u8"Y Compensate(μm)"));
    gridLayout->addWidget(label7, 0, 7, 1, 1);
    QLabel *label8 = new QLabel(this);
    label8->setText(tr(u8"Ruler Length(μm)"));
    gridLayout->addWidget(label8, 0, 8, 1, 1);

    QStringList list;
    list << "NULL" << "1X" << "2X" << "2.5X" << "4X" << "5X" << "10X" << "20X" << "40X" << "50X" << "80X" << "100X" << "150X";
    for(int i = 0; i < LENSCOUNT; i++)
    {
        comboBoxLens[i] = new QComboBox(this);
        comboBoxLens[i]->addItems(list);
        lineEditName[i] = new QLineEdit(this);
        lineEditUmPix[i] = new QLineEdit(this);
        lineEditNA[i] = new QLineEdit(this);
        lineEditWDC[i] = new QLineEdit(this);
        lineEditRulerLen[i] = new QLineEdit(this);
        lineEditXCompensate[i]  = new QLineEdit(this);
        lineEditYCompensate[i]  = new QLineEdit(this);
        lineEditName[i]->setText(tr("empty"));
        lineEditUmPix[i]->setText(u8"1");
        lineEditWDC[i]->setText("0");
        lineEditXCompensate[i]->setText("0");
        lineEditYCompensate[i]->setText("0");
        lineEditNA[i]->setText("1");
        lineEditRulerLen[i]->setText("100");

        gridLayout->addWidget(comboBoxLens[i], i+1, 1, 1, 1);
        gridLayout->addWidget(lineEditName[i], i+1, 2, 1, 1);
        gridLayout->addWidget(lineEditUmPix[i], i+1, 3, 1, 1);
        gridLayout->addWidget(lineEditNA[i], i+1, 4, 1, 1);
        gridLayout->addWidget(lineEditWDC[i], i+1, 5, 1, 1);
        gridLayout->addWidget(lineEditXCompensate[i], i+1, 6, 1, 1);
        gridLayout->addWidget(lineEditYCompensate[i], i+1, 7, 1, 1);
        gridLayout->addWidget(lineEditRulerLen[i], i+1, 8, 1, 1);
    }
    QVBoxLayout * verticalLayout = new QVBoxLayout(this);
    verticalLayout->addLayout(horizontalLayout);
    verticalLayout->addLayout(gridLayout);

    QHBoxLayout *horizontalLayout2 = new QHBoxLayout();
    QLabel *label9 = new QLabel(this);
    label9->setText(tr("Ruler Line Width"));
    horizontalLayout2->addWidget(label9);
    lineEditLineWidth = new QLineEdit(this);
    horizontalLayout2->addWidget(lineEditLineWidth);

    QLabel *label13 = new QLabel(this);
    label13->setText(tr("Color"));
    horizontalLayout2->addWidget(label13);
    comboBoxColor = new ColorComboBox(this);
    horizontalLayout2->addWidget(comboBoxColor);

    QLabel *label10 = new QLabel(this);
    label10->setText(tr("Ruler Font Size"));
    horizontalLayout2->addWidget(label10);
    lineEditFontSize = new QLineEdit(this);
    horizontalLayout2->addWidget(lineEditFontSize);

    QLabel *label11 = new QLabel(this);
    label11->setText(tr("Ruler Pos X"));
    horizontalLayout2->addWidget(label11);
    lineEditRulerPosX = new QLineEdit(this);
    horizontalLayout2->addWidget(lineEditRulerPosX);

    QLabel *label12 = new QLabel(this);
    label12->setText(tr("Ruler Pos Y"));
    horizontalLayout2->addWidget(label12);
    lineEditRulerPosY = new QLineEdit(this);
    horizontalLayout2->addWidget(lineEditRulerPosY);

    horizontalLayout2->addStretch();

    verticalLayout->addLayout(horizontalLayout2);


    QFrame * line = new QFrame(this);
    line->setFrameStyle(QFrame::HLine | QFrame::Sunken);
    verticalLayout->addWidget(line);

    buttonBox = new QDialogButtonBox(this);
    buttonBox->setOrientation(Qt::Horizontal);
    buttonBox->setStandardButtons(QDialogButtonBox::Apply | QDialogButtonBox::Cancel | QDialogButtonBox::Ok);

    verticalLayout->addWidget(buttonBox);

    QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    connect(buttonBox, &QDialogButtonBox::clicked, this , & LensManageDialog::onClicked);
}

void LensManageDialog::onClicked(QAbstractButton *button)
{
    if(buttonBox->buttonRole(button) == QDialogButtonBox::ApplyRole)
    {
        dumpLensData(lens);
        emit lensDataChanged();
    }
}
} //Qly
